/**
 *  登录表单控制器
 */
import tem from './index.html';
import $_fn from '~plugins'
class loginInputCtrl {
  constructor($scope,$rootScope,$timeout,$interval) {
    // let $scope = this
      this.rootfun = rootfun
      this.$timeout = $timeout
      let strParam = 'logIncodeImg'
      var token, timeSucc, timeErr
      //定义变量 用于添加class 高亮提醒用户（红色）
      $scope.error = {
          user:false,
          psw:false,
          code:false,
          phone:false,
          phoneCode:false,
          phoneCode1:false,
          resetpsw0:false,
          resetpsw1:false
      }
      //提示信息默认不显示 声明参数
      $scope.tishi = {
          succ:false,
          err:false
      }
      //  登录 声明参数 页面ng-model绑定传送数据到后台
      $scope.UnLogIn = {
          username:'',
          password:'',
          changeCode:'',
          token:''
      }
      //找回密码 页面ng-model绑定传送数据到后台
      $scope.findPsw = {
          phone:'',
          changeCode:'',
          token:''
      }
      //请输入手机验证码
      $scope.enterPsw = {
          phone:'',
          codes:''
      }
      //重置密码
      $scope.resetPsw = {
          psw0:'',
          psw1:'',
          phone:''
      }
      console.log(token, timeSucc, timeErr)
      
      //点击表单是隐藏错误信息提醒
      $scope.hideErr = function(){
          $('.tixingFalse').css({'right':'-250px'})
          $scope.tishi.err = false
          $scope.tishi.succ = false
          for(var item in $scope.error){
              $scope.error[item] = false
          }
      }
      //错误信息显示提醒方法
      $scope.errorFun = function(str,num){
          $('.errorMes').html( str )
          //传入1 是为了区分使用了ajax后 必须用$apply
          if(num == 1){
              $scope.tishi.err = true
              animation0()
          }else{
              $scope.$apply(function(){
                  $scope.tishi.err = true
              })
              animation0()
          }
      }
      //错误提醒动画
      function animation0(){
          $('.tixingFalse').animate({
              'right':'48px',
          },500,function(){
              clearTimeout(timeErr)
              timeErr = setTimeout(function(){
                  $('.tixingFalse').css({'right':'-250px'})
                  $scope.tishi.err = false
                  $scope.$apply()
              },5000)
          })
      }
      //成功信息显示提醒方法
      $scope.successFun = function(str,num){
          $('.succ').html( str )
          //传入1 是为了区分使用了ajax后 必须用$apply
          if(num == 1){
              $scope.tishi.succ = true
              animation1()
          }else{
              $scope.$apply(function(){
                  $scope.tishi.succ = true
              })
              animation1()
          }
      }
      //成功提醒动画
      function animation1(){
          $('.tixingTrue').animate({
              'right':'48px',
          },500,function(){
              clearTimeout(timeSucc)
              timeSucc = setTimeout(function(){
                  $('.tixingTrue').css({'right':'-250px'})
                  $scope.tishi.succ = false
                  $scope.$apply()
              },5000)
          })
      }
      //ajax回调函数
      function getCode(result){
          //闭包函数 实现局部传参
          return cloCallback(result,strParam)
      }
      // 回调函数的闭包函数
      function cloCallback(result,str){
          console.log('ryy-callback', result)
          $('.' + str).attr('src',result.content.img)
          //赋值token 重新发送给后端验证
          token = result.content.token
      }
      //btn点击获取验证码并写入img中   angular函数
      $scope.getLogInCode = function($event,params){
          if(params == 'logIncodeImg') {
              strParam = params//更改需要更新图片的节点
              $_fn.ajaxPostImg('imageCode.do','',getCode)
          }
          if(params == 'findPswImg'){
              strParam = params//更改需要更新图片的节点
              $_fn.ajaxPostImg('imageCode.do','',getCode)
          }
      }
      // 首次加载html发送ajax请求获取验证码
      $scope.defaultAjax = function(){
          console.log($_fn)
          $_fn.ajaxPostImg('imageCode.do','',getCode)
      }
      //登录、找回密码点击页面内跳转跳转   跳转发送ajax请求获取验证码
      $scope.logInLocationPath = function(params){

          // 回传登录成功模板切换
          $scope.$emit('template', params)
          if(params == 'findPsw'){
              strParam = 'findPswImg'//更改需要更新图片的节点
              $_fn.ajaxPostImg('imageCode.do','',getCode)
              console.log('ryy', 'findPsw')
          }
          if(params == 'logIn'){
              strParam = 'logIncodeImg'//更改需要更新图片的节点
              $_fn.ajaxPostImg('imageCode.do','',getCode)
          }
      }
      //键盘回车时提交表单
      $scope.key = function(e,param){
          var keycode = window.event?e.keyCode:e.which
          if(keycode == 13){                //回车触发
              if(param == 'logIn'){           //登录界面点击提交触发的方法
                  $scope.logInSuccess('logInSuccess')
              }
              if(param == 'find'){            //找回密码
                  $scope.findPswFun('enterPsw')
              }
              if(param == 'enter'){           //找回密码-输入验证码
                  $scope.enterPswFun()
              }
              if(param == 'reset'){           //找回密码-重置密码
                  $scope.resetPswFun()
              }
          }
      }

    var timer
    //请输入验证码 60s重新获取
    function interval(){
      $interval.cancel(timer)
      timer = $interval(function(){
        var num = $('._span').html()
        num--
        $('._span').html(num)
        if(num == 0){
          $interval.cancel(timer)
          $('.spanParent').css('display','none')
          $('.btnGet').css('display','block')
          $('._span').html('60')
        }
      },1000)
    }
    // 找回密码函数
    $scope.findPswFun = function(params){
      console.log($scope.findPsw.phone + '////' + $scope.findPsw.changeCode)
      if( /^\s*$/.test($scope.findPsw.phone) ){
        $scope.errorFun('手机号码不能为空',1)
        $scope.error.phone = true
        return
      }
      if( !(/^1[34578]\d{9}$/.test($scope.findPsw.phone) ) ){
        $scope.errorFun('手机号码不正确',1)
        $scope.error.phone = true
        return
      }
      if( /^\s*$/.test($scope.findPsw.changeCode) ){
        $scope.errorFun('验证码不能为空',1)
        $scope.error.phoneCode = true
        return
      }

      if(params == 'enterPsw'){
        $scope.findPsw.token = token           //获取后端的token 校验验证码
      }

      //找回密码发送ajax请求 把手机号码 验证码 token传给后台
      $_fn.ajaxPost('checkCaptcha.do',$scope.findPsw,function(result){
        console.log(result)
        if(result.type > 0){
          $scope.$emit('template', 'enterPsw');
          // $scope.logIn = 'work/tems2/logIn/enterPsw.html'//跳转到用户输入获取手机验证码页面
          $scope.enterPsw.phone = result.content//获取回来的手机号码绑定值到页面上
          $rootScope.userPhone = result.content
          $scope.successFun(result.msg)//验证成功提醒
          //验证通过后跳到用户获取手机验证码页面 再次发送ajax请求 把手机号码传给后台
          $_fn.ajaxPost('sendmsg.do',{phone:$scope.enterPsw.phone},function(result){
            console.log(result)
            interval()
          })
          $scope.$apply()
        }else{
          $scope.errorFun(result.msg)          //后台返回数据 错误信息调用动画提醒用户
          //Message('F',result.msg)
        }
      })
    }

  }
}
loginInputCtrl.$inject = ['$scope','$rootScope','$timeout','$interval']
module.exports = angular.module('directive.findPsw', []).directive('findPsw', function() {
    return {
        restrict: 'E',
        template: tem,
        controller: loginInputCtrl
    }
}).name